package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GwCustomerInfo;
import com.ruoyi.system.domain.vo.EnterpriseInfoVO;
import com.ruoyi.system.mapper.GwEnterpriseInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GwInvestorInfo;
import com.ruoyi.system.service.IGwInvestorInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 股东/合伙人管理Controller
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@Api(description = "股东/合伙人管理")
@RestController
@RequestMapping("/system/investor")
public class GwInvestorInfoController extends BaseController {

    @Autowired
    private IGwInvestorInfoService gwInvestorInfoService;
    @Resource
    private GwEnterpriseInfoMapper gwEnterpriseInfoMapper;

    /**
     * 查询股东/合伙人管理列表
     */
    @ApiOperation("查询股东/合伙人管理列表")
//    @PreAuthorize("@ss.hasPermi('system:investor:list')")
    @GetMapping("/list")
    public TableDataInfo list(GwInvestorInfo gwInvestorInfo) {
        startPage();
        List<GwInvestorInfo> list = gwInvestorInfoService.selectGwInvestorInfoList(gwInvestorInfo);
        return getDataTable(list);
    }

    /**
     * 查询股东/合伙人管理列表不分页
     */
    @ApiOperation("查询股东/合伙人管理列表, status: 在册、历史")
    @GetMapping("/getList")
    public AjaxResult getList(GwInvestorInfo gwInvestorInfo) {
        if (ObjectUtils.isEmpty(SecurityUtils.getLoginUser()) || ObjectUtils.isEmpty(SecurityUtils.getLoginUser().getEnterpriseId())) {
            return success(new ArrayList<>());
        }

        List<GwInvestorInfo> list = gwInvestorInfoService.selectGwInvestorInfoList(gwInvestorInfo);

        EnterpriseInfoVO enterpriseInfo = gwEnterpriseInfoMapper.getLoginEnterpriseInfo(SecurityUtils.getLoginUser().getEnterpriseId());
        GwInvestorInfo investorInfo = new GwInvestorInfo();
        investorInfo.setId(0L);
        investorInfo.setInvestorName(enterpriseInfo.getEnterpriseName());
        list.add(0, investorInfo);
        return success(list);
    }

    /**
     * 获取股东/合伙人管理详细信息
     */
    @ApiOperation("获取股东/合伙人管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:investor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(gwInvestorInfoService.selectGwInvestorInfoById(id));
    }

    /**
     * 导出股东/合伙人管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:investor:export')")
    @Log(title = "股东/合伙人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwInvestorInfo gwInvestorInfo) {
        List<GwInvestorInfo> list = gwInvestorInfoService.selectGwInvestorInfoList(gwInvestorInfo);
        ExcelUtil<GwInvestorInfo> util = new ExcelUtil<GwInvestorInfo>(GwInvestorInfo.class);
        util.exportExcel(response, list, "股东/合伙人管理数据");
    }

    /**
     * 新增股东/合伙人管理
     */
    @ApiOperation("新增股东/合伙人管理")
//    @PreAuthorize("@ss.hasPermi('system:investor:add')")
    @Log(title = "股东/合伙人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GwInvestorInfo gwInvestorInfo) {
        if ("企业".equals(gwInvestorInfo.getInvestorType())) {
            if (ObjectUtils.isEmpty(gwInvestorInfo.getSocialCode()) || gwInvestorInfo.getSocialCode().length() > 20) {
                return AjaxResult.error("统一社会信用代码必填，限输入20个字符");
            }
        }
        return toAjax(gwInvestorInfoService.insertGwInvestorInfo(gwInvestorInfo));
    }

    /**
     * 修改股东/合伙人管理
     */
    @ApiOperation("修改股东/合伙人管理")
//    @PreAuthorize("@ss.hasPermi('system:investor:edit')")
    @Log(title = "股东/合伙人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GwInvestorInfo gwInvestorInfo) {
        if ("企业".equals(gwInvestorInfo.getInvestorType())) {
            if (ObjectUtils.isEmpty(gwInvestorInfo.getSocialCode()) || gwInvestorInfo.getSocialCode().length() > 20) {
                return AjaxResult.error("统一社会信用代码必填，限输入20个字符");
            }
        }
        return toAjax(gwInvestorInfoService.updateGwInvestorInfo(gwInvestorInfo));
    }

    /**
     * 删除股东/合伙人管理
     */
    @ApiOperation("删除股东/合伙人管理")
//    @PreAuthorize("@ss.hasPermi('system:investor:remove')")
    @Log(title = "股东/合伙人管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(gwInvestorInfoService.deleteGwInvestorInfoByIds(ids));
    }
}
